
class State
  
  attr_accessor :enter_func, :leave_func, :pawn, :parent_state
  
  def enter
    @enter_func.call if @enter_func
  end
  
  def leave
    @leave_func.call if @leave_func
  end
  
  def exit_state
    if @parent_state then
      $game.enter_state @parent_state
    else
      # no parent state? we're the root state! and we're closing! bye!
      $game.close
    end
		
  end
  
end

def init_states

  init_main_state
	init_game_state
	init_lab_state
  
end

def init_main_state
		# set up our main menu
  menu = Menu.new
  menu.add MenuItem.new("New Mission") { $game.enter_state :game_state}
  menu.add MenuItem.new("Vehicle Lab") {$game.enter_state :lab_state}
	menu.add MenuItem.new("Level Editor") {Flash.new 0xaaffffff, 0.2}
  menu.add MenuItem.new("Quit") {log 'Quitting'; $game.close}
  
  # set up our game states
  main_menu = State.new
  main_menu.enter_func = lambda { 
    log 'Enter main state'
    $game.player.pawn = menu
    menu.active = true    
  }
  main_menu.leave_func = lambda { 
    log 'Leave main state'
    menu.active = false
  }
  $game.states[:main_menu] = main_menu
end

def init_game_state
	game_state = State.new
  game_state.enter_func = lambda {
    Flash.new(0xff000000, 0.5)
    $game.load_map #$game.map
  }
  game_state.leave_func = lambda {
		Flash.new(0xff000000, 0.5)
    $game.unload_map
  }
  game_state.parent_state = :main_menu
  $game.states[:game_state] = game_state
end

def init_lab_state
	
	lab_state = State.new
	lab_select_chassis_state = State.new
	lab_modify_vehicle_state = State.new
	lab_modify_section_state = State.new
	
	$game.states[:lab_state] = lab_state
	$game.states[:lab_select_chassis] = lab_select_chassis_state
	$game.states[:lab_modify_vehicle] = lab_modify_vehicle_state
	$game.states[:lab_modify_section] = lab_modify_section_state

	lab_menu = Menu.new
	lab_menu.add MenuItem.new("New Vehicle") {}#$game.enter_state :lab_select_chassis}
	lab_menu.add MenuItem.new("Back") {$game.enter_state :main_menu}
	
	lab_state.enter_func = lambda {
		$game.player.pawn = lab_menu
		lab_menu.active = true
	}
	
	lab_state.leave_func = lambda {
		lab_menu.active = false
	}
	
end